import { defineStore } from 'pinia';
import { Api } from '../script/Api';

const store = defineStore('ipinfo', {
  state() {
    return {
      info: {
        // 是否查询到信息
        having: false,
        // ip相关信息
        ip: {},
        // 天气相关信息
        weather: {},
      },
    };
  },
  actions: {
    queryInfo() {
      let instance = this;
      // 获取ip信息
      Api.get('/tools/ipinfo', {}, (ipdata) => {
        if (!ipdata.success) {
          instance.info.having = false;
          return;
        }
        // ip信息
        instance.info.ip = ipdata.data;
        // 获取天气信息
        Api.get(
          '/tools/weather/live',
          {
            city: ipdata.data.citycode,
          },
          (weatherdata) => {
            if (!weatherdata.success) {
              instance.info.having = false;
              return;
            }
            instance.info.weather = weatherdata.data;
            instance.info.having = true;
          }
        );
      });
    },
  },
});

export default store;
